<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * 2015-11-21
 */
namespace Mobile\Controller;

use Home\Logic\UsersLogic;
use Think\Page;
use Think\Verify;

class YuyueController extends MobileBaseController
{

    public function _initialize()
    {
        parent::_initialize();
        $this->assign('navid', "yuyue"); //存储用户信息
    }

    /**
     * 教练员列表
     */

    public function coach_list()
    {


        $cate_id=I('cate_id');
        $sql = " select *  from __PREFIX__coach WHERE is_open>0 AND (cate_id=$cate_id OR cate_id2=$cate_id OR cate_id3=$cate_id)";

        $coach_list=M()->query($sql);
//        dump($coach_list);exit;

        if(count($coach_list)<1){
            $msg ='该课程暂无教练！';
            $this->error($msg,U('User/my_kc'));
        }

      /*  //查询该用户购买的私教课程
        $user_id = $_SESSION['user']['user_id'];
        if (!$user_id) {
            $msg = '预约需要先登录，请登录！';
            $this->error($msg, U('User/login'));
        }
        $where['user_id'] = $user_id;
        $where['num'] = array('gt', 0);
        $yy_auth_num_list = M('yy_auth_num')->where($where)->select();
        $coach_cate_str = '';
        foreach ($yy_auth_num_list AS $v) {
            $coach_cate_str ? $coach_cate_str .= ',' . $v['coach_cate_id'] : $coach_cate_str .= $v['coach_cate_id'];
        }

      if($coach_cate_str ==''){
            $msg ='如需要预约，请购买1对1私教课程';
            $this->error($msg,U('Goods/categoryList'));
         }
        //查询对应的教练员
        $n_where['cate_id']=array('in',$coach_cate_str);
        $n_where['is_open']=1;
        $coach_list = M('coach')->where($n_where)->select();*/
        $this->assign('cate_id', $cate_id);
        $this->assign('lists', $coach_list);
        $this->display();
    }


    /**
     * 教练员详情
     */

    public function coach_detail()
    {
        $weekarray = array("日", "一", "二", "三", "四", "五", "六");
        $date_arr[] = date("Ymd");
        $date_arr[] = date("Ymd", strtotime('+1 day'));
        $date_arr[] = date("Ymd", strtotime('+2 day'));
        if ($_SESSION['is_coach']) {
            $coach_info = M('coach')->where(array('status' => 1, 'user_id' => $_SESSION['user']['user_id']))->find();
            $coach_id = $coach_info['id'];
            $this->assign('is_coach', 1);
        } else {
            $coach_id = I('coach_id');
            $coach_info = M('coach')->where(array('id' => $coach_id))->find();
        }

        $date_str = I('date_str');
        if (!$date_str)
            $date_str = $date_arr[0];

        if($date_str==$date_arr[0]){
            //过时时段

            //获取当前时间
           $dq_h= date("H",time());
           $yy_sd_list=M('yytime')->select();
            $gs_arr=array();
           foreach($yy_sd_list AS $kk1=>$vv1){
              $arr1= explode('-',$vv1['value']);
              $arr2= explode(':',$arr1[0]);
              $n_time=$arr2[0];
                if($n_time>$dq_h){
                }else{
                    $gs_arr[]=$vv1['id'];
                }
           }
        }

        //被预约了的时段
        $where['yy_date'] = $date_str;
        $where['coach_id'] = $coach_id;
        $where['type_id'] = 1;
        $coach_yy_list = M('coach_yy')->where($where)->field('time_id')->select();
        $coach_yy_arr = array();
        foreach ($coach_yy_list AS $v) {
            $coach_yy_arr[] = $v['time_id'];
        }
        //教练设置忙的时段
        $where['type_id'] = 2;
        $coach_ms_list = M('coach_yy')->where($where)->field('time_id')->select();
        $coach_ms_arr = array();
        foreach ($coach_ms_list AS $vv) {
            $coach_ms_arr[] = $vv['time_id'];
        }
        //预约时段信息
        $yy_time = M('yytime')->where(array('status' => 1))->order('id ASC')->select();

        $this->assign('weekarray', $weekarray);
        $this->assign('coach_id', $coach_id);
        $this->assign('date_str', $date_str);
        $this->assign('date_arr', $date_arr);
        $this->assign('lists', $yy_time);
        $this->assign('coach_yy_arr', $coach_yy_arr);
        $this->assign('gs_arr', $gs_arr);
        $this->assign('coach_ms_arr', $coach_ms_arr);
        $this->assign('coach_info', $coach_info);
        $this->assign('cate_id', I('cate_id'));
        $this->display();

    }


    public function yy_coach()
    {
        $coach_id = I('coach_id');
        $cate_id = I('cate_id');
        $yytime_id = I('yytime_id');
        $yy_date = I('new_date_str');
        $type = I('type');
        $user_id = $_SESSION['user']['user_id'];
        $user_info = get_user_info($user_id);
        if (count($yytime_id) > 0) {
            foreach ($yytime_id AS $v) {
                $data['user_id'] = $user_id;
                $data['coach_id'] = $coach_id;
                $data['yy_date'] = $yy_date;
                $data['coach_cate_id'] = $cate_id;
                $data['time_str'] = get_yy_time_str($v);
                $data['addtime'] = time();
                $data['time_id'] = $v;
                $data['status'] = 1;
                $data['tel'] = $user_info['mobile'];
                if ($_SESSION['is_coach']) {//教练员预约时段忙时设定
                    $re_msg = '设定成功';
                    $data['type_id'] = 2;
                    M('coach_yy')->add($data);
                } else {   //会员预约教练员


//                  $coach=M('coach')->where("id=$coach_id")->find();
                  $auth_where['user_id']=$user_id;
                  $auth_where['coach_cate_id']=$cate_id;
                    $info = M('yy_auth_num')->where($auth_where)->find();
                    if ($info['num'] < 1) {
                        unset($data);
                        $this->error('可预约数量已用完，请购买。', U("Yuyue/coach_detail/coach_id/$coach_id/cate_id/$cate_id"));
                        exit;
                    }
                    $auth_num_id=$info['id'];
                    M('yy_auth_num')->where("id=$auth_num_id")->setDec('num', 1);
                    $re_msg = '预约成功';
                    $data['type_id'] = 1;
                    $data['yy_id'] = 'Y.' . date('YmdHis') . rand(1000, 9999);
                    $id=M('coach_yy')->add($data);

                   //查询教练用户信息
                    $coach=M('coach')->where("id=$coach_id")->find();
                    $c_user_id=$coach['user_id'];
                    $c_user=M('users')->where("user_id=$c_user_id")->find();
                    //查询用户信息
                    $u_user=M('users')->where("user_id=$user_id")->find();
                    //预约信息
                     $yy_info=M('coach_yy')->where("id=$id")->find();
                     $wx_user = M('wx_user')->find();
                    $yy_type=get_coach_cate_name($yy_info['coach_cate_id']);//预约类别信息
                    $jssdk = new \Mobile\Logic\Jssdk($wx_user['appid'],$wx_user['appsecret']);
                    //预约成功后发送微信推文给用户
                    /* {{first.DATA}}
                        预约课程：{{keyword1.DATA}}
                        预约俱乐部：{{keyword2.DATA}}
                        预约教练：{{keyword3.DATA}}
                        预约时间：{{keyword4.DATA}}
                        {{remark.DATA}}*/
                    $wx_data=array(
                        'first'=>array('value'=>"一对一私教预约成功",'color'=>"#000"),
                        'keyword1'=>array('value'=>$yy_type,'color'=>'#000'),
                        'keyword2'=>array('value'=>'中山市恒力健身馆','color'=>'#000'),
                        'keyword3'=>array('value'=> $coach['name'].'('.$coach['tel'].')','color'=>'#000'),
                        'keyword4'=>array('value'=>'日期：'.$yy_info['yy_date'].'('.$yy_info['time_str'].')','color'=>'#DF0000'),
                        'remark'=>array('value'=>"\n点击可查看订单详情内容.",'color'=>'#C5C5C5'),
                    );
                    $other_parm=array(
                        'url'=>'https://www.hengli118.com/Mobile/User/my_yy.html',
                        'templateid'=>'RO4I9PeiYdD3I8jgSDSmAAM3E3lv3560YXEEuwmciM8',
                        'topcolor'=>'#ff0'
                    );
                    $jssdk->push_msg_template($u_user['openid'],$wx_data,$other_parm);
                    unset($wx_data);
                    unset($other_parm);

                    //预约成功后发送微信推文给教练
                   /* {{first.DATA}}
                    学员姓名：{{keyword1.DATA}}
                    下单时间：{{keyword2.DATA}}
                    联系方式：{{keyword3.DATA}}
                    课程信息：{{keyword4.DATA}}
                    {{remark.DATA}}*/
                    $wx_data=array(
                        'first'=>array('value'=>"您有新的被预约信息",'color'=>"#000"),
                        'keyword1'=>array('value'=>$u_user['nickname'],'color'=>'#000'),
                        'keyword2'=>array('value'=>date('Y-m-d H:i:s',$yy_info['addtime']),'color'=>'#000'),
                        'keyword3'=>array('value'=>'手机号：'.$u_user['mobile'],'color'=>'#000'),
                        'keyword4'=>array('value'=>'日期：'.$yy_info['yy_date'].'('.$yy_info['time_str'].')|课程类别：'.$yy_type,'color'=>'#DF0000'),
                        'remark'=>array('value'=>"\n点击可查看订单详情内容.",'color'=>'#C5C5C5'),
                    );
                    $other_parm=array(
                        'url'=>'https://www.hengli118.com/Mobile/User/my_b_yy.html',
                        'templateid'=>'_ePu2XgGIibxuSbrkd9ghRBDnmJBk_qKeu3d-Zx9a5A',
                        'topcolor'=>'#ff0'
                    );
                    $jssdk->push_msg_template($c_user['openid'],$wx_data,$other_parm);
                    unset($wx_data);
                    unset($other_parm);
                }
                unset($data);
            }

            $this->success($re_msg);
        } else {
            $this->error('请选择预约时段');
        }
    }


    /**
     * 取消预约
     */
    public function cancel_yy(){
        $id=I('id');
        $yy_info =M('coach_yy')->where("id=$id")->find();
        $end_time = end( explode('-',$yy_info['time_str']));
        $a= strtotime($yy_info['yy_date'].$end_time);
        /*   if(time()>$a){
               //更改状态为已完成
               $up_data['id']=$v['id'];
               $up_data['status']=3;
                M('coach_yy')->save($up_data);
           }else*/
        if($a-time()>3600*5){//时段结束前5小时可取消预约
            //把该条状态的状态改为取消
            $up_data['id']=$id;
            $up_data['status']=2;
            $up_data['type_id']=3;
            $res=  M('coach_yy')->save($up_data);
            $res=1;
            if($res){
                //增加该用户的的本类目的可预约数量1次
                $coach_id=$yy_info['coach_id'];
                $user_id=$yy_info['user_id'];
                $coach=M('coach')->where("id=$coach_id")->find();
                $up_n_where['coach_cate_id']=$coach['cate_id'];
                $up_n_where['user_id']=$yy_info['user_id'];
                M('yy_auth_num')->where($up_n_where)->setInc('num',1);


                //查询教练用户信息
                $coach=M('coach')->where("id=$coach_id")->find();
                $c_user_id=$coach['user_id'];
                $c_user=M('users')->where("user_id=$c_user_id")->find();
                //查询用户信息
                $u_user=M('users')->where("user_id=$user_id")->find();
                //预约信息
                $yy_info=M('coach_yy')->where("id=$id")->find();
                $wx_user = M('wx_user')->find();
                $yy_type=get_coach_cate_name($yy_info['coach_cate_id']);//预约类别信息
                $jssdk = new \Mobile\Logic\Jssdk($wx_user['appid'],$wx_user['appsecret']);
                //取消预约后发送微信推文给用户
                /* {{first.DATA}}
                课程类型：{{keyword1.DATA}}
                课程时间：{{keyword2.DATA}}
                场馆地址：{{keyword3.DATA}}
                {{remark.DATA}}*/
                $wx_data=array(
                    'first'=>array('value'=>"私教预约取消通知",'color'=>"#000"),
                    'keyword1'=>array('value'=>$yy_type,'color'=>'#000'),
                    'keyword2'=>array('value'=>'日期：'.$yy_info['yy_date'].'('.$yy_info['time_str'].')','color'=>'#000'),
                    'keyword3'=>array('value'=>'中山市恒力健身馆','color'=>'#000'),
                    'remark'=>array('value'=>"取消后，教练将可被更多会员预约。",'color'=>'#C5C5C5'),
                );
                $other_parm=array(
                    'url'=>'',
                    'templateid'=>'oOO0YJ5iRaPj9tVB_9WEWyS8qo748RXwnL9d3ZV8V0A',
                    'topcolor'=>'#ff0'
                );
                $jssdk->push_msg_template($u_user['openid'],$wx_data,$other_parm);
                unset($wx_data);
                unset($other_parm);

                //取消预约后发送微信推文给教练
                /* {{first.DATA}}
                    会员姓名：{{keyword1.DATA}}
                    联系电话：{{keyword2.DATA}}
                    取消课程：{{keyword3.DATA}}
                    预约时间：{{keyword4.DATA}}
                    取消时间：{{keyword5.DATA}}
                    {{remark.DATA}}*/
                $wx_data=array(
                    'first'=>array('value'=>"私教预约取消通知",'color'=>"#000"),
                    'keyword1'=>array('value'=>$u_user['nickname'],'color'=>'#000'),
                    'keyword2'=>array('value'=>$u_user['mobile'],'color'=>'#000'),
                    'keyword3'=>array('value'=>$yy_type,'color'=>'#000'),
                    'keyword4'=>array('value'=>'日期：'.$yy_info['yy_date'].'('.$yy_info['time_str'].')','color'=>'#DF0000'),
                    'keyword5'=>array('value'=>date("Y-m-d H:i:s",time()),'color'=>'#DF0000'),
                    'remark'=>array('value'=>"预约已取消，如有疑问请与该用户联系",'color'=>'#C5C5C5'),
                );
                $other_parm=array(
                    'url'=>'',
                    'templateid'=>'3q6xMXXagJ13nFVRQJKuqj4fBnyc9FEepJU2pmSkzdk',
                    'topcolor'=>'#ff0'
                );
                $jssdk->push_msg_template($c_user['openid'],$wx_data,$other_parm);
                unset($wx_data);
                unset($other_parm);



                $this->success('已成功取消。');
            }else{
                $this->error('取消失败，请重试！');
            }
        }else{
            $this->error('该条预约不可取消。');
        }
    }






}
